package com.uenpay.im.utilities;

import com.qyx.android.utilities.DateTimeUtility;
import com.qyx.android.utilities.TimeUtility;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;

import java.text.MessageFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author SL
 * @created 2016-3-26 上午11:35:22
 * @copyRight http://www.qiyunxin.com
 * @function 时间管理
 */
public class QyxDateUtils {

	public static String generateDateTimeSecondString(Date date) {
		final String dateFormat = "yyyy-MM-dd";
		Date now = new Date(System.currentTimeMillis());
		String targetDateString = DateTimeUtility.getDateTimeString(date,
				dateFormat);
		Calendar targetCalendar = DateTimeUtility.convertDateToCalendar(date);

		if (DateTimeUtility.getDateTimeString(now, dateFormat).equals(
				targetDateString)) {
			// today
			return getTimeSectionString(
					DateTimeUtility.getDateTimeString(date, "HH:mm:ss"),
					targetCalendar);
		} else if (DateTimeUtility.getDateTimeString(
				TimeUtility.getYesterdayCalendar().getTime(), dateFormat)
				.equals(targetDateString)) {
			// yesterday
			return IMApplication.getAppContext().getResources()
					.getString(R.string.yesterday)
					+ getTimeSectionString(
							DateTimeUtility.getDateTimeString(date, "HH:mm:ss"),
							targetCalendar);
		} else {
			if (date.getTime() > TimeUtility.getFirstDayOfWeekCalendar()
					.getTime().getTime()) {
				// in this week
				return getWeekDateString(targetCalendar)
						+ " "
						+ getTimeSectionString(
								DateTimeUtility.getDateTimeString(date,
										"HH:mm:ss"), targetCalendar);
			} else if (targetCalendar.get(Calendar.YEAR) != DateTimeUtility
					.convertDateToCalendar(now).get(Calendar.YEAR)) {
				// different year
				return targetDateString;
			} else {
				// same year
				return DateTimeUtility
						.getDateTimeString(date, "MM-dd HH:mm:ss");
			}
		}
	}

	public static String generateDateTimeString(Date date) {

		final String dateFormat = "yyyy-MM-dd";
		Date now = new Date(System.currentTimeMillis());
		String targetDateString = DateTimeUtility.getDateTimeString(date,
				dateFormat);
		Calendar targetCalendar = DateTimeUtility.convertDateToCalendar(date);

		if (DateTimeUtility.getDateTimeString(now, dateFormat).equals(
				targetDateString)) {
			// today
			return getTimeSectionString(
					DateTimeUtility.getDateTimeString(date, "HH:mm"),
					targetCalendar);
		} else if (DateTimeUtility.getDateTimeString(
				TimeUtility.getYesterdayCalendar().getTime(), dateFormat)
				.equals(targetDateString)) {
			// yesterday
			return IMApplication.getAppContext().getResources()
					.getString(R.string.yesterday)
					+ getTimeSectionString(
							DateTimeUtility.getDateTimeString(date, "HH:mm"),
							targetCalendar);
		} else {
			if (date.getTime() > TimeUtility.getFirstDayOfWeekCalendar()
					.getTime().getTime()) {
				// in this week
				return getWeekDateString(targetCalendar)
						+ " "
						+ getTimeSectionString(
								DateTimeUtility
										.getDateTimeString(date, "HH:mm"),
								targetCalendar);
			} else if (targetCalendar.get(Calendar.YEAR) != DateTimeUtility
					.convertDateToCalendar(now).get(Calendar.YEAR)) {
				// different year
				return targetDateString;
			} else {
				// same year
				return DateTimeUtility.getDateTimeString(date, "MM-dd HH:mm");
			}
		}

	}

	private static String getTimeSectionString(String time, Calendar calendar) {
		int hour = calendar.get(Calendar.HOUR_OF_DAY);
		String str = "";
		if (hour < 12) {
			str = IMApplication.getAppContext().getResources()
					.getString(R.string.morning);
		} else if (hour >= 12 && hour < 13) {
			str = IMApplication.getAppContext().getResources()
					.getString(R.string.noon);
		} else if (hour >= 13 && hour < 18) {
			str = IMApplication.getAppContext().getResources()
					.getString(R.string.afternoon);
		} else {
			str = IMApplication.getAppContext().getResources()
					.getString(R.string.evening);
		}
		return MessageFormat.format(str, time);
	}

	public static String getWeekDateString(Calendar calendar) {
		switch (calendar.get(Calendar.DAY_OF_WEEK)) {
		case 1:
			return IMApplication.getAppContext().getResources()
					.getString(R.string.Sunday);
		case 2:
			return IMApplication.getAppContext().getResources()
					.getString(R.string.Monday);
		case 3:
			return IMApplication.getAppContext().getResources()
					.getString(R.string.Tuesday);
		case 4:
			return IMApplication.getAppContext().getResources()
					.getString(R.string.Wednesday);
		case 5:
			return IMApplication.getAppContext().getResources()
					.getString(R.string.Thursday);
		case 6:
			return IMApplication.getAppContext().getResources()
					.getString(R.string.Friday);
		case 7:
			return IMApplication.getAppContext().getResources()
					.getString(R.string.Saturday);
		default:
			return "";
		}
	}

}
